from datetime import datetime

from django.urls import reverse
from rest_framework.test import APITestCase

# from backend.django_res.orm.models import BuyOrderModel


# Create your tests here.


class BuyOrderViewsTest(APITestCase):
    def setUp(self):
        self.url = reverse('buy_order')

    def test_create(self):
        data={'buy_date_time': datetime.now(),
              'digest': 'flowers',
              'amount': 1,
              'price': 35,
              'unit': '束'}
        resp = self.client.post(self.url, data=data)
        self.assertEqual(resp.status_code, 201)
        self.assertEqual('flowers', resp.data['digest'])
        self.assertEqual('System default supplier', resp.data['supplier_detail']['name'])
        order_id = resp.data['id']

        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 200)
        self.assertTrue(isinstance(resp.data, list))
        self.assertEqual(1, len(resp.data))

        resp = self.client.get(f'{self.url}{order_id}/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual('flowers', resp.data['digest'])
        self.assertEqual('System default supplier', resp.data['supplier_detail']['name'])

        not_exist_id = order_id + 1
        resp = self.client.get(f'{self.url}{not_exist_id}/')
        self.assertEqual(resp.status_code, 400)

        resp = self.client.delete(f'{self.url}{not_exist_id}/')
        self.assertEqual(resp.status_code, 400)

        resp = self.client.delete(f'{self.url}{order_id}/')
        self.assertEqual(resp.status_code, 204)

        resp = self.client.get(self.url)
        self.assertEqual(resp.status_code, 200)
        self.assertTrue(isinstance(resp.data, list))
        self.assertEqual(0, len(resp.data))
